package com.bawei.lib_storage.room

import android.content.Context
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

object DBUtils {
    lateinit var db: AppDataBase

    fun init(context: Context,dbName:String){
        val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE b_takevideo ADD COLUMN title VARCHAR(25) NOT NULL ")
            }
        }
        
        val MIGRATION_2_3 = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为b_history表添加userId字段，默认为空字符串
                database.execSQL("ALTER TABLE b_history ADD COLUMN userId VARCHAR(50) NOT NULL DEFAULT ''")
            }
        }
        
        val MIGRATION_3_4 = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 创建收藏表
                database.execSQL("""
                    CREATE TABLE IF NOT EXISTS b_favorite (
                        id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                        userId TEXT NOT NULL,
                        videoId TEXT NOT NULL,
                        title TEXT NOT NULL,
                        primaryImg TEXT NOT NULL,
                        content TEXT NOT NULL,
                        authorImg TEXT NOT NULL,
                        authorName TEXT NOT NULL,
                        videoPath TEXT NOT NULL,
                        channelId TEXT NOT NULL,
                        createTime INTEGER NOT NULL DEFAULT 0
                    )
                """)
            }
        }
        
        val MIGRATION_4_5 = object : Migration(4, 5) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为b_history表添加progress字段，默认为0
                database.execSQL("ALTER TABLE b_history ADD COLUMN progress INTEGER NOT NULL DEFAULT 0")
            }
        }
        
        val MIGRATION_5_6 = object : Migration(5, 6) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为b_history表添加lastPosition字段，默认为0
                database.execSQL("ALTER TABLE b_history ADD COLUMN lastPosition INTEGER NOT NULL DEFAULT 0")
            }
        }
        
        val MIGRATION_6_7 = object : Migration(6, 7) {
            override fun migrate(database: SupportSQLiteDatabase) {
                // 为b_history表添加videoId字段，默认为空字符串
                database.execSQL("ALTER TABLE b_history ADD COLUMN videoId TEXT NOT NULL DEFAULT ''")
            }
        }
        
        db = Room.databaseBuilder(context, AppDataBase::class.java, dbName)
//            .openHelperFactory(SupportFactory(SQLiteDatabase.getBytes("123456".toCharArray())))
            .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, MIGRATION_5_6, MIGRATION_6_7)
            .build()
    }

    fun getDB(): AppDataBase {
        if (db ==  null){
            throw NullPointerException("请先初始化数据库")
        }
        return db
    }
}